package com.laiyy.gitee.feign.springcloudfeignhttpclient.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.Executor;

/**
 * 拦截全局-异步调用-发生的异常
 */
@Configuration
@EnableAsync
@Slf4j
public class CatchExceptionConfiguration extends AsyncConfigurerSupport {

    @Override
    public Executor getAsyncExecutor() {
        return new SimpleAsyncTaskExecutor();
    }


    @Override
    @Nullable
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        //lambda表达式用作异常处理程序
        return (throwable, method, obj) -> {
            log.error("异常所在线程 ：{},异常信息 ：{},异常方法名称 ：{}", Thread.currentThread().getName(), throwable.getMessage(), method.getName());
            for (Object param : obj) {
                log.error("异常参数 ：" + param);
            }
        };
    }
}
